var values;
var hitOptions;
var segment, path;
var movePath, currentKey;
var selectedObject = null;

function change(a) {
    tol = a;
    switch (tol) {
    case 'a':
        tool.minDistance = 10;
        tool.maxDistance = 10;
        movePath = false;
        //log("mudou para a");
        break;
    case 'b':
        tool.minDistance = 10;
        movePath = false;
        //log("mudou para b");
        break;
    case 'c':
        movePath = true;
        hitOptions = {
            segments: true,
            stroke: true,
            fill: true,
            tolerance: 5
        };
        break;
    }
}
change("a");

function onKeyDown(event) {
    currentKey = event.key;
    // When a key is pressed, set the content of the text item:
    switch (tol) {
    case 'a':

        break;
    case 'b':

        break;
    case 'c':
        if (event.key == "delete") {
            selectedObject.remove();
        }
        break;
    }
}

function onKeyUp(event) {
    // When a key is released, set the content of the text item:
}

function onMouseMove(event) {
    switch (tol) {
    case 'a':

        break;
    case 'b':

        break;
    case 'c':
        break;
    }
}

function onMouseUp(event) {
var hitResult = project.hitTest(event.point, hitOptions);

    switch (tol) {
    case 'a':
        log("a_up");
        a_up(event);
        break;
    case 'b':

        break;
    case 'c':

        break;
    }
}


function onMouseDrag(event) {
    switch (tol) {
    case 'a':
        log("a_drag");
        a_drag(event);
        break;
    case 'b':
        log("b_drag");
        b_drag(event);
        break;
    case 'c':
        log("c_drag");
        c_drag(event);
        break;
    }
}

function onMouseDown(event) {
    switch (tol) {
    case 'a':
        log("a_down");
        a_down(event);
        break;
    case 'b':
        log("b_down");
        b_down(event);
        break;
    case 'c':
        log("c_down");
        c_down(event);
        break;
    }
}

function onFrame(event) {
    change(tol);
}
//tool A

function a_down(event) {
    path = new Path();
    path.strokeColor = new HsbColor(Math.random() * 360, 1, 1);
    path.strokeWidth = 10;

}

function a_drag(event) {
    path.add(event.point);
    
}

function a_up(event) {
path.simplify();
}


//tool B



function b_down(event) {
    path = new Path();
    path.strokeColor = '#00000';

    path.add(event.point);
}

function b_drag(event) {
    path.add(event.point);

    var step = event.delta;
    step.angle += 90;

    var top = event.middlePoint + step;
    var bottom = event.middlePoint - step;

    var line = new Path();
    line.strokeColor = '#000000';
    line.add(top);
    line.add(bottom);
}

//tool C








function c_down(event) {
    segment = path = null;

    var hitResult = project.hitTest(event.point, hitOptions);
    project.activeLayer.selected = false;

    if (hitResult && hitResult.item) {
        if (selectedObject) {
            selectedObject.selected = false;
        }
        hitResult.item.selected = true;
        hitResult.item.fullySelected = true;
        selectedObject = hitResult.item;
    } else {
        selectedObject = null;
    }

    if (selectedObject) {
        if (event.modifiers.control) {
            if (hitResult.type == 'segment') {
                hitResult.segment.remove();
            };
            return;
        }

        if (hitResult) {
            path = hitResult.item;
            if (hitResult.type == 'segment') {
                segment = hitResult.segment;
            } else if (hitResult.type == 'stroke' && event.modifiers.shift) {
                var location = hitResult.location;
                segment = path.insert(location.index + 1, event.point);
                path.smooth();
            }
        }
        movePath = hitResult.type == 'fill';
        if (movePath) project.activeLayer.addChild(hitResult.item);
    }
}

function c_drag(event) {
    if (segment && event.modifiers.shift) {
        segment.point = event.point;
        path.smooth();
    } else if (movePath) {
        path.position += event.delta;
    }
}

